#include <corona.h>
#include "Texture.h"



cTexture::cTexture(void)
{
}

cTexture::~cTexture(void)
{
}

bool cTexture::Load(std::string lsFilename,int liType,int liWraps,int liWrapt,int liMagf,int liMinf,bool lbMipmap)
{
	corona::Image* lsImg;
	int liComponents;

  lsImg = corona::OpenImage(lsFilename.c_str());
  msName = lsFilename;
	if(liType==GL_RGB)
	{
		//img = corona::OpenImage(filename,corona::PF_R8G8B8);
		liComponents = 3;
	}
	else if(liType==GL_RGBA)
	{
		//img = corona::OpenImage(filename,corona::PF_R8G8B8A8);
		liComponents = 4;
	}
	else return false;

	if(lsImg==NULL) return false;

	muiWidth  = lsImg->getWidth();
	muiHeight = lsImg->getHeight();

	glGenTextures(1, &mguiId);
	glBindTexture(GL_TEXTURE_2D,mguiId);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,liWraps);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,liWrapt);

	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,liMagf);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,liMinf);

	if(!lbMipmap)
	{
		glTexImage2D(GL_TEXTURE_2D,0,liComponents,muiWidth,muiHeight,0,liType,
              GL_UNSIGNED_BYTE,lsImg->getPixels());
	}
	else
	{
		gluBuild2DMipmaps(GL_TEXTURE_2D,liComponents,muiWidth,muiHeight,liType,
						  GL_UNSIGNED_BYTE,lsImg->getPixels());
	}

	return true;
}
int cTexture::GetID()
{
	return mguiId;
}

void cTexture::GetSize(unsigned int *luiWidth,unsigned int *luiHeight)
{
	*luiWidth = muiWidth;
	*luiHeight = muiHeight;
}

std::string cTexture::GetName()
{
  return msName;
}
